$(function(){
	function getTableSource(){
		var tableSource = new $.jqx.dataAdapter({
			dataType: "json",
			dataFields:[
			    { name: "id", type: "int" },
			    { name: "black_type", type: "int" },
			    { name: "black_value", type: "string" },
			    { name: "isenable", type: "int" },
			    { name: "creation_time", type: "string" },
			    { name: "creation_user", type: "string" },
			    { name: "remark", type: "string" }
			],
			id: "id",
			url: "blacklist/getBlackList.do"
		});
		return tableSource;
	}
	
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: getTableSource(),
        pageable: true,
        serverProcessing: true,
        pageSize: 18,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width: '16.6%', cellsRenderer: function(row) { return (row + 1).toString(); } },
            { text: "黑名单类型", dataField: "black_type", width: '14.28%',cellsRenderer: function(row, column, value, rowData){
        		return ["邮箱号黑名单","手机号黑名单"][value];
        	}},
            { text: "黑名单值", dataField: "black_value", width: '14.28%' },
            { text: "是否启用", dataField: "isenable", width: '14.28%', cellsRenderer: function(row, column, value, rowData){
            	return ["未启用","启用"][value];
            }},
            { text: "添加时间", dataField: "creation_time", width: '14.28%' },
            { text: "添加人", dataField: "creation_user", width: '14.28%'},
            { text: "备注", dataField: "remark", width: '14.29%'}
        ],
	});
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;
	 	_editerIndex = layer.open({
			id: "sellerEditer",
			title: "查看/修改黑名单信息",
		    type: 1,
		    skin: 'layui-layer-rim',
		    area: ['1000px', '340px'],
		    content:
		    info.args.type =="add"?
		    '<form id="blacklist-editer" class="form" method="post" style="height: 100%;overflow: auto;">' +
		    '<div id="order_editer" class="form" style="overflow: auto;">' +
			'<input type="hidden" name="id" id="id"/>' +
			'<label class="from-label" for="black_type">黑名单类型：</label>' +
			'<select name="black_type" id="black_type" class="form-control">' +
			'<option value="1">手机号黑名单</option><option value="0">邮箱黑名单</option></select>' + 
			'<label class="from-label" for="black_value">黑名单值：</label>' +
			'<input type="text" name="black_value" id="black_value" allowBlank="false" class="form-control" placeholder="黑名单值" />' +
			'<label class="from-label" for="isenable">是否启用：</label>' +
			'<select name="isenable" id="isenable" class="form-control">' +
			'<option value="1">启用</option><option value="0">未启用</option></select>' +
			'<label class="from-label" for="remark">备注：</label>' +
			'<input type="text" name="remark" id="remark" allowBlank="false" class="form-control" placeholder="备注" />' +
			'<input type="hidden" name="creation_user" id="creation_user" allowBlank="false" class="form-control" placeholder="创建人" value="admin" />' +
			'</div>' +
			'<div id="detailed-datatable"' +
			'	style="border-radius: 0; min-height: 500px; margin: 0 0 0 32px;"></div>' +
			'<br> <br> <br>' +
			'</form>'+
			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
    		'<input type="button" class="btn btn-sm btn-success" onclick="saveData(1);" value="保存"/>' +
    		'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this, true)" value="返回"/>' +
    		'</div>':
    			'<form id="blacklist-editer" class="form" method="post" style="height: 100%;overflow: auto;">' +
    		    '<div id="order_editer" class="form" style="overflow: auto;">' +
    			'<input type="hidden" name="id" id="id" />' +
    			'<input type="hidden" name="submit-url" value="blacklist/updateBlack.do" />' +
    			'<label class="from-label" for="black_type">黑名单类型：</label>' +
    			'<select name="black_type" id="black_type" class="form-control">' +
    			'<option value="1">手机号黑名单</option><option value="0">邮箱黑名单</option></select>' + 
    			'<label class="from-label" for="black_value">黑名单值：</label>' +
    			'<input type="text" name="black_value" id="black_value" allowBlank="false" class="form-control" placeholder="黑名单值" />' +
    			'<label class="from-label" for="isenable">是否启用：</label>' +
    			'<select name="isenable" id="isenable" class="form-control">' +
    			'<option value="1">启用</option><option value="0">未启用</option></select>' +
    			'<label class="from-label" for="remark">备注：</label>' +
    			'<input type="text" name="remark" id="remark" allowBlank="false" class="form-control" placeholder="备注" />' +
    			'<input type="hidden" name="creation_user" id="creation_user" allowBlank="false" class="form-control" placeholder="创建人" value="admin" />' +
    			'</div>' +
    			'<div id="detailed-datatable"' +
    			'	style="border-radius: 0; min-height: 500px; margin: 0 0 0 32px;"></div>' +
    			'<br> <br> <br>' +
    			'</form>'+
    			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
        		'<input type="button" class="btn btn-sm btn-success" onclick="saveData(2);" value="保存"/>' +
        		'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this, true)" value="返回"/>' +
        		'</div>'
		});
	 	
	 	$("input[name='id']").val(editingData['id']);
	 	setData($("#blacklist-editer"), editingData);
	});
});

function saveData(type){
	var blackType = $("#black_type").val();
	var blackValue = encodeURI(encodeURI($("#black_value").val()));
	var isenable = $("#isenable").val();
	var remark = encodeURI(encodeURI($("#remark").val()));
	var creationUser = encodeURI(encodeURI($("#creation_user").val()));
	var addData = "";
	switch (type) {
	case 2:
		url = "blacklist/updateBlack.do";
		addData = {
			"id" : $("#id").val(),
			"black_type" : blackType,
			"black_value" : blackValue,
			"isenable" : isenable,
			"remark" : remark,
			"creation_user" : creationUser
		};
		break;
	case 1:
	default:
		url = "blacklist/addBlack.do";
		addData = {
			"black_type" : blackType,
			"black_value" : blackValue,
			"isenable" : isenable,
			"remark" : remark,
			"creation_user" : creationUser
		};
	}
	$.ajax({
		url : url,
		type : "post",
		data : addData,
		dataTypa : "json",
		success : function(res) {
			layer.msg(res.msg);
			layer.close(_editerIndex);
			update($("#dataTable"));
		}
	});
}

/**
 * 是否选中一项
 * 
 * @returns
 */
function isSelected() {
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length < 1) {
		layer.msg("请选择一行数据", {
			shift : 6,
			offset : "5%"
		});
		return null;
	}
	return rows;
}